<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生管理</title>
    <link rel="stylesheet" href="/static/css/base.css">
    <link rel="stylesheet" href="/static/css/head.css">
    <style>
        .main {
            padding-top: 20px;
        }

        .main .wrapper {
            display: flex;
            justify-content: space-between;
        }

        .main .wrapper .left {
            flex: 1;
            margin-right: 20px;
        }

        .main .wrapper .right {
            flex: 1;
        }

        .main .wrapper .panel {
            border-color: #BCE8F1;
        }

        .main .wrapper .panel h3 {
            background-color: #D9EDF7;
            color: #31708F;
            text-align: center;
        }

        .left .down {
            margin-top: 30px;
        }

        .left .down button {
            width: 100%;
            height: 46px;
            font-size: 18px;
        }

        .student-edit {
            width: 500px;
        }
        .student-edit .stu_status {
            text-align: left;
            margin-left: 105px;
        }
        .student-edit .stu_status label {
            margin-right: 20px;
        }
    </style>
</head>
<body>
{% include 'common/admin_head.html' %}
<div class="main">
    <div class="wrapper">
        <div class="left">
            <div class="up">
                <div class="student-list panel">
                    <h3 class="panel-title">学生管理</h3>
                    <div class="panel-body">
                        <table class="table">
                            <thead>
                            <tr>
                                <th>编号</th>
                                <th>学生名称</th>
                                <th>学生学号</th>
                                <th>创建时间</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody id="student">
                            <!-- 课程数据 -->
                            </tbody>
                        </table>
                        <ul class="pagination" id="student-page">
                            <!-- 分页导航 -->
                        </ul>
                    </div>
                </div>
            </div>
            <div class="down">
                <button type="button" class="btn" id="student-add">添加学生</button>
            </div>
        </div>
        <div class="right">
            <div class="student-list panel">
                <h3 class="panel-title">学生选课申请管理</h3>
                <div class="panel-body">
                    <table class="table">
                        <thead>
                        <tr>
                            <th>编号</th>
                            <th>学生名称</th>
                            <th>课程名称</th>
                            <th>开课日期</th>
                            <th>结课日期</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody id="student-course-select">
                        <!-- 课程数据 -->
                        </tbody>
                    </table>
                    <ul class="pagination" id="student-course-select-page">
                        <!-- 分页导航 -->
                    </ul>
                </div>
            </div>
        </div>
    </div>
    <div class="wrap-dialog hide" id="student-edit-dialog">
        <div class="student-edit dialog">
            <div class="dialog-body">
                <form class="student-form" id="student-form">
                    <div class="input-box">
                        <label class="input-box-label" for="student_name">学生名称</label><span
                            class="input-box-span"><input
                            class="input-box-span-input" type="text" id="student_name"
                            placeholder="请输入学生名称"
                            name="student_name"></span>
                    </div>
                    <div class="input-box">
                        <label class="input-box-label" for="student_num_id">学生学号</label><span
                            class="input-box-span"><input
                            class="input-box-span-input" type="text" id="student_num_id"
                            placeholder="请输入学生学号"
                            name="student_num_id"></span>
                    </div>
                    <div class="stu_status">
                        <input type="radio" name="student_status" id="radio1" value="1" checked> <label
                            for="radio1">正常</label>
                        <input type="radio" name="student_status" id="radio2" value="0"><label
                            for="radio2">禁用</label>
                    </div>
                    <input type="hidden" id="student_id" name="student_id">
                </form>
            </div>
            <div class="dialog-footer">
                <input type="button" class="btn submit" id="student-submit" value="提交"/>
                <input type="button" class="btn cancel" value="关闭"/>
            </div>
        </div>
    </div>
    {% include 'common/change_password.html' %}
    {% include 'common/pop_message.html' %}
</div>
<script src="/static/jquery-3.7.1.min.js"></script>
<script src="/static/change_pwd.js"></script>
<script>
    $(document).ready(function () {
        studentList(1)
        studentCourseList(1)

        // 学生列表切换页码
        $('#student-page').on('click', 'li:not(.active, .disabled, .pp)', function () {
            studentList($(this).attr('page'));
        });

        $('#student-course-select-page').on('click', 'li:not(.active, .disabled, .pp)', function () {
            studentCourseList($(this).attr('page'));
        });

        // 打开修改学生窗口
        $('#student').on('click', 'button.btn-success', function () {
            $('#student-edit-dialog').removeClass('hide');
            $('#student-submit').attr('operation', 'update');
            getStudent($(this).attr('id_val'));
        });

        // 删除学生
        $('#student').on('click', 'button.btn-danger', function () {
            var id = $(this).attr('id_val');
            console.log(id)
            studentDelete(id);
        })

        // 删除学生选课申请
        $('#student-course-select').on('click', 'button.btn-danger', function () {
            var id = $(this).attr('id_val');
            studentCourseDelete(id);
        })

        // 通过学生选课申请
        $('#student-course-select').on('click', 'button.btn-success', function () {
            var id = $(this).attr('id_val');
            studentCoursePass(id);
        })

        // 打开添加学生窗口
        $('#student-add').on('click', function () {
            $('#student-edit-dialog').removeClass('hide');
            $('#student_name').val('')
            $('#student_num_id').val('')
            $('#student_id').val('')
            $('#radio1').prop('checked', true)
            $('#student-submit').attr('operation', 'add');
        })

        $('#student-submit').on('click', function () {
            var operation = $(this).attr('operation');
            if (operation === 'add') {
                // 添加学生
                studentAdd();
            } else {
                // 修改学生
                studentUpdate();
            }
            $('#student-edit-dialog').addClass("hide");
        })

        // 搜索
        $('#search').on('click', function () {
            studentList(1)
            studentCourseList(1)
        });

        $('#student-edit-dialog .cancel').click(function () {
            $('#student-edit-dialog').addClass("hide");
        });

        // 关闭提示框
        $('.close').click(function () {
            $('#pop-message-dialog').addClass('hide');
        });
    });

    // 学生列表分页查询
    function studentList(page) {
        var search = $('#search-input').val();
        $.ajax({
            url: '/user/role',
            type: 'GET',
            data: {'page': page, 'page_size': 3, 'role': 'student', 'search': search},
            success: function (response) {
                console.log(response)
                if (response.code === 200) {
                    var userList = response.data['user_list'];
                    var pages = response.data['pages'];
                    console.log(userList)
                    console.log(pages)
                    // 更新列表
                    var data_html = '';
                    for (var i = 0; i < userList.length; i++) {
                        var user = userList[i];
                        data_html += '<tr>\n' +
                            '    <th scope="row">' + (i + 1) + '</th>\n' +
                            '    <td>' + user.username + '</td>\n' +
                            '    <td>' + user.number_id + '</td>\n' +
                            '    <td>' + user.create_time + '</td>\n' +
                            '<td style="color: ' + ((user.status === 1) ? "green" : "red") + ';">' + ((user.status === 1) ? "正常" : "禁用") + '</td>\n' +
                            '    <td>\n' +
                            '        <button class="btn btn-success" id_val="' + user.id + '">修改</button>\n' +
                            '        <button class="btn btn-danger" id_val="' + user.id + '">删除</button>\n' +
                            '    </td>\n' +
                            '</tr>';
                    }
                    $('#student').html(data_html);
                    // 更新分页控制
                    $('#student-page').empty()
                    if (pages.is_prev === 1) {
                        $('#student-page').append('<li class="pp">\n' +
                            '<a href="javascript:prevStu()" aria-label="Previous">\n' +
                            '    <span aria-hidden="true">&laquo;</span>\n' +
                            '</a>\n' +
                            '</li>')
                    }
                    for (var i = 0; i < pages.range.length; i++) {
                        if (pages.current === pages.range[i]) {
                            $('<li>').attr('page', pages.range[i]).addClass('active').append(
                                $('<a>').attr('href', '#').text(pages.range[i])).appendTo('#student-page')
                        } else {
                            $('<li>').attr('page', pages.range[i]).append(
                                $('<a>').attr('href', '#').text(pages.range[i])).appendTo('#student-page')
                        }
                    }
                    if (pages.is_next === 1) {
                        $('#student-page').append(
                            '<li class="pp">\n' +
                            '    <a href="javascript:nextStu()" aria-label="Next">\n' +
                            '        <span aria-hidden="true">&raquo;</span>\n' +
                            '    </a>\n' +
                            '</li>'
                        )
                    }
                } else {
                    console.log("查询失败")
                }
            },
            error: function (xhr, status, error) {
                console.error('请求失败: ', status, error);
            }
        });
    }

    function prevStu() {
        var page = $('#student-page .active').attr('page');
        studentList(parseInt(page) - 1);
    }

    function nextStu() {
        var page = $('#student-page .active').attr('page')
        studentList(parseInt(page) + 1);
    }

    // 学生详情
    function getStudent(id) {
        $.ajax({
            url: '/user/get',
            type: 'GET',
            data: {'id': id},
            success: function (response) {
                //console.log(response)
                if (response.code === 200) {
                    var user = response.data['user'];
                    $('#student_id').val(user.id)
                    $('#student_name').val(user.username);
                    $('#student_num_id').val(user.number_id);
                    console.log("status----", user.status)
                    console.log("status----", typeof user.status)
                    console.log("status----", user.status === 1)
                    if (user.status === 1) {
                        $('#radio1').prop('checked', true)
                    } else {
                        $('#radio2').prop('checked', true)
                    }
                }
            },
            error: function (xhr, status, error) {
                console.error('请求失败: ', status, error);
            }
        });
    }

    // 删除学生
    function studentDelete(id) {
        $.ajax({
            url: '/user/delete',
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({'id': id}),
            success: function (response) {
                console.log(response)
                if (response.code === 200) {
                    studentList(1)
                } else {
                    $('#pop-message-dialog p').text(response.msg);
                    $('#pop-message-dialog').removeClass("hide");
                }
            },
            error: function (xhr, status, error) {
                console.error('请求失败: ', status, error);
            }
        });
    }

    // 添加学生
    function studentAdd() {
        var student_name = $('#student_name').val();
        var student_num_id = $('#student_num_id').val();
        var student_status = 1
        if ($('#radio1').is(':checked')) {
            student_status = 1
        } else {
            student_status = 0
        }

        $.ajax({
            url: '/user/add',
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({
                'username': student_name,
                'role': 'student',
                'number_id': student_num_id,
                'status': student_status
            }),
            success: function (response) {
                //console.log(response)
                if (response.code === 200) {
                    studentList(1)
                } else {
                    console.log("添加失败")
                }
            },
            error: function (xhr, status, error) {
                console.error('请求失败: ', status, error);
            }
        });
    }

    // 修改学生
    function studentUpdate() {

        var student_id = $('#student_id').val();
        var student_name = $('#student_name').val();
        var student_num_id = $('#student_num_id').val();
        var student_status = 1
        if ($('#radio1').is(':checked')) {
            student_status = 1
        } else {
            student_status = 0
        }
        $.ajax({
            url: '/user/update',
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({
                'id': student_id,
                'username': student_name,
                'role': 'student',
                'number_id': student_num_id,
                'status': student_status,
            }),
            success: function (response) {
                //console.log(response)
                if (response.code === 200) {
                    studentList(1)
                } else {
                    console.log("修改失败")
                }
            },
            error: function (xhr, status, error) {
                console.error('请求失败: ', status, error);
            }
        });
    }

    // 学生选课申请分页查询
    function studentCourseList(page) {
        var search = $('#search-input').val();
        $.ajax({
            url: '/student-course/select',
            type: 'GET',
            data: {'page': page, 'page_size': 3, 'search': search},
            success: function (response) {
                console.log(response)
                if (response.code === 200) {
                    var scList = response.data['student_course_list'];
                    var pages = response.data['pages'];
                    console.log(scList)
                    console.log(pages)
                    // 更新列表
                    var data_html = '';
                    for (var i = 0; i < scList.length; i++) {
                        var sc = scList[i];
                        data_html += '<tr>\n' +
                            '                                    <th scope="row">' + (i + 1) + '</th>\n' +
                            '                                    <td>' + sc.username + '</td>\n' +
                            '                                    <td>' + sc.course_name + '</td>\n' +
                            '                                    <td>' + sc.start_date + '</td>\n' +
                            '                                    <td>' + sc.end_date + '</td>\n' +
                            '                                    <td>\n' +
                            '                                        <button class="btn btn-success" ' + (sc.status == 1 ? "disabled" : "") + ' id_val="' + sc.id + '">通过</button>\n' +
                            '                                        <button class="btn btn-danger" id_val="' + sc.id + '">删除</button>\n' +
                            '                                    </td>\n' +
                            '                                </tr>';
                    }
                    $('#student-course-select').html(data_html);
                    // 更新分页控制
                    $('#student-course-select-page').empty()
                    if (pages.is_prev === 1) {
                        $('#student-course-select-page').append('<li class="pp">\n' +
                            '<a href="javascript:prevStuC()">\n' +
                            '    <span>&laquo;</span>\n' +
                            '</a>\n' +
                            '</li>')
                    }
                    for (var i = 0; i < pages.range.length; i++) {
                        if (pages.current === pages.range[i]) {
                            $('<li>').attr('page', pages.range[i]).addClass('active').append(
                                $('<a>').attr('href', '#').text(pages.range[i])).appendTo('#student-course-select-page')
                        } else {
                            $('<li>').attr('page', pages.range[i]).append(
                                $('<a>').attr('href', '#').text(pages.range[i])).appendTo('#student-course-select-page')
                        }
                    }
                    if (pages.is_next === 1) {
                        $('#student-course-select-page').append(
                            '<li class="pp">\n' +
                            '    <a href="javascript:nextStuC()">\n' +
                            '        <span>&raquo;</span>\n' +
                            '    </a>\n' +
                            '</li>'
                        )
                    }
                } else {
                    console.log("查询失败")
                }
            },
            error: function (xhr, status, error) {
                console.error('请求失败: ', status, error);
            }
        });
    }

    function prevStuC() {
        var page = $('#student-course-select-page .active').attr('page');
        studentCourseList(parseInt(page) - 1);
    }

    function nextStuC() {
        var page = $('#student-course-select-page .active').attr('page')
        studentCourseList(parseInt(page) + 1);
    }

    // 通过学生的选课申请
    function studentCoursePass(id) {
        $.ajax({
            url: '/student-course/select-pass',
            type: 'GET',
            data: {'id': id},
            success: function (response) {
                console.log(response)
                if (response.code === 200) {
                    studentCourseList(1)
                } else {
                    $('#pop-message-dialog p').text(response.msg);
                    $('#pop-message-dialog').removeClass("hide");
                }
            },
            error: function (xhr, status, error) {
                console.error('请求失败: ', status, error);
            }
        });
    }

    // 删除学生选课申请
    function studentCourseDelete(id) {
        $.ajax({
            url: '/student-course/delete',
            type: 'GET',
            data: {'id': id},
            success: function (response) {
                console.log(response)
                if (response.code === 200) {
                    studentCourseList(1)
                } else {
                    $('#pop-message-dialog p').text(response.msg);
                    $('#pop-message-dialog').removeClass("hide");
                }
            },
            error: function (xhr, status, error) {
                console.error('请求失败: ', status, error);
            }
        });
    }
</script>
</body>
</html>
